package com.trsvax.gwthello.client.widget;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.i18n.client.Dictionary;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.rpc.ServiceDefTarget;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.trsvax.gwthello.client.ButtonService;
import com.trsvax.gwthello.client.ButtonServiceAsync;
import com.trsvax.gwthello.client.Tapestry;

public class ButtonWidget extends Composite {
	private final ButtonServiceAsync buttonService = GWT.create(ButtonService.class);
	String id;

	public ButtonWidget(String id, Dictionary dictionary) {
		this.id = id;
		((ServiceDefTarget) buttonService).setServiceEntryPoint(dictionary.get("completeID"));

		Button b = new Button("Jump!", new ClickHandler() {
			public void onClick(ClickEvent event) {

				reset(7);
			}
		});
		initWidget(b);

	}

	void reset(Integer value) {
		buttonService.reset(value, new AsyncCallback<String>() {
			public void onFailure(Throwable caught) {

			}

			public void onSuccess(String result) {
				Tapestry.loadZone(id, "/index:clicker");

			}
		});
	}
}
